package com.agou.app.admin.business.user.bo;

import java.io.IOException;
import java.sql.SQLException;

import com.agou.app.AppContext;
import com.agou.app.admin.business.user.vo.UserVo;
import com.agou.app.admin.dbconfig.bean.TSAdminUser;
import com.agou.app.admin.dbconfig.bean.TSAdminUserLog;
import com.agou.app.admin.dbconfig.bean.TSAdminUserSecurity;
import com.agou.app.admin.enums.EnAdminUserLogEventResult;
import com.agou.app.admin.enums.EnAdminUserLogEventType;
import com.agou.app.admin.enums.EnAdminUserSecurityType;
import com.agou.app.admin.enums.EnAdminUserState;
import com.agou.core.ss.SSException;
import com.agou.core.ss.SSTrace;
import com.agou.core.util.UtFormat;
import com.agou.core.util.UtSecurity;
import com.agou.core.util.UtSequence;
import com.agou.ssh.core.base.BOResultSet;
import com.agou.ssh.core.db.field.DateField;
import com.agou.ssh.web.WebUtil;

public class UserBo extends IUserBo{

	private static SSTrace ssTrace = SSTrace.getInstance(UserBo.class);
	/**
	 * 用户登陆
	 */
	public BOResultSet userLogin(UserVo uservo) throws SSException,
			SQLException, IOException {
		ssTrace.debug("userLogin", "开始执行用户登陆：#0", uservo);
		BOResultSet resultSet = new BOResultSet(this.getClass());
		TSAdminUser tAdminUser = null;
		/** 表单数据检查 **/
		if (uservo.userLoginId == null
				|| uservo.userLoginId.trim().length() == 0) {
			resultSet.IsSuccess = false;
			resultSet.ReturnCode = "-2";
			resultSet.ReturnInfo = "用户登陆账号为空.";
			return resultSet;
		}
		if (uservo.password == null || uservo.password.trim().length() == 0) {
			resultSet.IsSuccess = false;
			resultSet.ReturnCode = "-2";
			resultSet.ReturnInfo = "用户密码为空.";
			return resultSet;
		}
		this.getUserDao().setJdbcSession(this.getJdbcSession());// 设置数据库环境
		/** 检查用户是否存在 **/
		tAdminUser = getUserDao().queryUserByUserLoginId(uservo.userLoginId);
		if (tAdminUser == null) {// 用户不存在
			resultSet.IsSuccess = false;
			resultSet.ReturnCode = "-3";
			resultSet.ReturnInfo = "用户不存在.";
			return resultSet;
		}
		resultSet.addDataMap("tsAdminUser", tAdminUser);
		/** 检查用户密码是否正确 **/
		TSAdminUserSecurity tAdminUserSecurity = getUserDao().queryUserSecurity(tAdminUser.getUserId());//
		if (tAdminUserSecurity == null) {
			resultSet.IsSuccess = false;
			resultSet.ReturnCode = "-4";
			resultSet.ReturnInfo = "用户未设置安全策略.";
			saveUserLog(EnAdminUserLogEventType.LoginUser,EnAdminUserLogEventResult.Success, "用户[#0]未设置安全策略:" ,tAdminUser.getUserLoginId());
			return resultSet;
		}
		resultSet.addDataMap("tsAdminUserSecurity", tAdminUserSecurity);
		// 对当前密码进行MDS加密
		String md5pass = UtSecurity.md5Coding(uservo.getPassword());
		if (md5pass.equals(tAdminUserSecurity.getPassword()) == false) {
			resultSet.IsSuccess = false;
			resultSet.ReturnCode = "-5";
			resultSet.ReturnInfo = "密码错误.";
			saveUserLog(EnAdminUserLogEventType.LoginUser,EnAdminUserLogEventResult.Success, "用户[#0]密码错误[#1]",uservo.getUserLoginId(),uservo.getPassword());
			return resultSet;
		}
		// 用户登陆
		saveUserLog(EnAdminUserLogEventType.LoginUser,EnAdminUserLogEventResult.Success, "用户[#0]登陆成功:",uservo.getUserLoginId());
		return resultSet;
	}

	// 用户注册
	public BOResultSet userRegister(UserVo uservo) throws SSException,
			SQLException, IOException {
		BOResultSet resultSet = new BOResultSet(this.getClass());//返回结果集
		getUserDao().setJdbcSession(this.getJdbcSession());//数据DAO对象使用数据操作
		// 检测用户是否存在
		TSAdminUser adminUser = getUserDao().queryUserByUserLoginId(uservo.getUserLoginId());
		if(adminUser != null){//查询有记录
			ssTrace.debug("userLogin", "用户已存在");
			resultSet.IsSuccess = false;
			resultSet.ReturnInfo = "注册用户已存在";
			return resultSet;
		}
		adminUser = new TSAdminUser();
		//adminUser.setUserId(UtSequence.genRandomGUID());
		adminUser.setUserId(UtSequence.genRandomNum());
		adminUser.setUserLoginId(uservo.getUserLoginId());
		adminUser.setUserName(uservo.getUserLoginId());
		adminUser.setIsValid(EnAdminUserState.Valid.getCode());//0：无效 1：有效
		adminUser.setRemark("备注信息");//备注
		adminUser.setCreateDate(new DateField());
		getUserDao().saveUser(adminUser);
		//设置安全信息
		TSAdminUserSecurity userSecurityTs = new TSAdminUserSecurity();
		userSecurityTs.setUserId(adminUser.getUserId());
		userSecurityTs.setPassCreateDate(new DateField());
		userSecurityTs.setPassUpdateDate(new DateField());
		userSecurityTs.setPassCoding("MD5");
		userSecurityTs.setPassword(UtSecurity.md5Coding("111111"));
		userSecurityTs.setSecurityType(EnAdminUserSecurityType.Password.getCode());
		this.getUserDao().saveUserSecurity(userSecurityTs);
		//日志表中记录用户操作
		saveUserLog(EnAdminUserLogEventType.CreateUser,EnAdminUserLogEventResult.Success,"用户[#0]注册成功",uservo.getUserLoginId());
		return resultSet;
	};
	
	// 保存用户日志
	private void saveUserLog(EnAdminUserLogEventType eventType,
			EnAdminUserLogEventResult eventResult, String otherkey,Object... params) 
		throws SSException, SQLException, IOException {
		ssTrace.debug("saveUserLog", "保存用户操作[#0]日志", eventType.getName());
		AppContext ctx = WebUtil.getAppContext();
		TSAdminUserLog userLog = new TSAdminUserLog();
		userLog.setLogDate(new DateField());
		userLog.setEventType(eventType.getCode());// 事件类型
		userLog.setEventDesc(eventType.getName());
		userLog.setEventResultCode(eventResult.getCode());
		userLog.setEventResultName(eventResult.getName());
		userLog.setOtherKey(UtFormat.format(otherkey, params) );
		if (ctx != null) {
			userLog.setUserId(ctx.userId);
			userLog.setUserLoginId(ctx.userLoginId);
			userLog.setUserName(ctx.userName);
			userLog.setOptIp(ctx.remoteHost);
			userLog.setOptSession(ctx.sessionId);
		} else {
			userLog.setUserId(-1);
			userLog.setUserLoginId("_");
			userLog.setUserName("_");
			userLog.setOptIp("_");
			userLog.setOptSession("_");
		}
		this.getUserDao().saveUserLog(userLog);
	}

}
